/*******************************************************************************
* Long-term effects of weather-induced migration on urban labor and housing 
  markets
* Busso & Chauvin

* Purpose: Implement balance tests from Borusyak et al.

*******************************************************************************/

/******************************************************************************/
/* Version that controls for Macroregion fixed effects 
(as in Borusyak et al's example) */
/******************************************************************************/ 
/* This section is based on the code by Borusyak et al. on Autor et al. data */ 
use $db_out/shift_share_db_cities.dta, clear

keep if year == 1991

local balancevars imf_sh igv_sh isv_sh sh_emp_col l_pop_all91 pop_growth8091 shift_8190_dws_t_rur
local basecontrols city_macrofe_1 city_macrofe_2 city_macrofe_3 city_macrofe_4 city_macrofe_5

/* Normalize z, get conventional SE */ 
sum shift_0009_dws_t_rur 
local sd_z = r(sd)
replace shift_0009_dws_t_rur = (shift_0009_dws_t_rur-r(mean))/r(sd)
qui foreach v of local balancevars {
	ivreg2 `v' shift_0009_dws_t_rur `basecontrols', r
	local convse_`v' = _se[shift_0009_dws_t_rur]
}

/* Ssaggregate */ 
ssaggregate `balancevars' shift_0009_dws_t_rur, n(mca8010) s(city_rur_mig_91_sh) ///
l(mcarp8010) sfile($db_out/shift_share_db_shares.dta) controls("`basecontrols'") 

/* Bring-in the droughts variable */ 
merge 1:1 mca8010 using $db_out/shift_share_db_rural.dta
keep if _merge==3
drop _merge
merge 1:1 mca8010 using $db_out/cluster_vars
keep if _merge==3
drop _merge

/* Run regression */ 
mat define tmat = J(7, 3, .)
local contador = 0

foreach v in imf_sh isv_sh igv_sh sh_emp_col l_pop_all91 pop_growth8091 shift_8190_dws_t_rur  {
	local contador = `contador' + 1
	ivreg `v' (shift_0009_dws_t_rur = drought_dws0009) [aw=s_n], cluster(mcm8010)
	matrix tmat[`contador', 1] = r(table)[1, 1]
	matrix tmat[`contador', 2] = r(table)[2, 1]
	matrix tmat[`contador', 3] = r(table)[4, 1]
}

/* Format table */ 
clear
svmat2 tmat, rnames(var) names(col)

drop var

gen s1 = "*" if c3 < 0.1
replace s1 = "**" if c3 < 0.05
replace s1 = "***" if c3 < 0.01
gen coef1 = string(c1, "%20.3fc")
replace coef1 = coef1 + s1 
gen coef2 = string(c2, "%20.3fc")
replace coef2 = "(" + coef2 + ")" 

keep coef*

gen outcome = ""
order outcome
replace outcome = "~~~~~Share of manufacturing in employment" if _n == 1
replace outcome = "~~~~~Share of services in employment" if _n == 2
replace outcome = "~~~~~Share of government in employment" if _n == 3
replace outcome = "Share of college-educated in employment" if _n == 4
replace outcome = "Log of population" if _n == 5
replace outcome = "Lagged population growth" if _n == 6
replace outcome = "Lagged shift-share IV" if _n == 7

insobs 2, before(1)
replace outcome = "\cmidrule{2-3}\textbf{Panel A: Base-year variables (1991)}" if _n == 1
replace outcome = "\textit{Local industry structure}" if _n == 2
insobs 2, before(8)
replace outcome = "\textbf{Panel B: Lagged changes (1980-1991)}" if _n == 9

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 4, before(1)
replace latex = "\begin{tabular}{lcc}" in 1
replace latex = "\toprule & \multirow{2}[0]{*}{Coefficient} & Exposure-robust \\" in 2
replace latex = "& & standard error \\" in 3
replace latex = "& (1) & (2) \\" in 4

insobs 2, after(15)
replace latex = "\bottomrule & & \\" in 16
replace latex = "\end{tabular}" in 17

cap file close myfile
file open myfile using "$da_out/tables/table_1.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile